package cn.flying.cloud.base.common.utils.local;

import java.util.Objects;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池工具类
 *
 * @author: admin
 * @date: 2022年11月10日 19:34
 * @version: 1.0
 */
public class ExecutorUtil {

    private static ThreadPoolExecutor executor;

    static {
        int processors = Runtime.getRuntime().availableProcessors();
        if (Objects.isNull(executor)) {
            executor = new ThreadPoolExecutor(processors * 2, processors * 4, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(200));
        }
    }

    public static Future<?> execute(Runnable runnable) {
        return executor.submit(runnable);
    }

    public static ThreadPoolExecutor getExecutor() {
        return executor;
    }
}
